<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyleTree.html">
</head>
<body>
<script type="text/html" id="headerManagementRole">
    <form class="layui-form" action="" lay-filter="queryForm" style="margin-top:10px">
        <div class="table-top-filter" style="position: relative;">
            <div class="table-filter-alone">
                <label class="layui-form-label">角色</label>
                <input type="hidden" name="roleList">
                <input style="width:180px" type="text" class="layui-input filter-value" name="Q_roleName_SL" placeholder="角色"/>
            </div>
        </div>
        <a class="layui-btn layui-btn-sm" lay-event="query" style="position: absolute;top: 4px;right: 95px;">
            <i class="layui-icon layui-icon-search"></i>
            <span>查询</span>
        </a>
    </form>
</script>
<div name="roleSelectorDiv">
    <div class="left">
        <span style="background: #e6e6e6;width: 100%;height: 30px;line-height: 30px;padding-left:12px;display: inline-block;">按组织查找</span>
        <ul id="orgTree" class="ztree"></ul>
    </div>
    <div class="center">
        <div class="filter-table" style="position: relative;">
            <table class="layui-hide" id="roleListTable" lay-filter="roleListTable"></table>
            <span style="position:absolute;right:-30px;top:10px;width:30%">已选择</span>
            <div style="height: 474px;overflow: auto;position:absolute;right:-30px;top:38px;width:30%">
                <table class="layui-table" id="RoleApps" style="margin: 0">
                    <tbody class="contentDivs">
                    <tr>
                        <td></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'form', 'laydate', 'totalFilter','setter','commons','common'], function () {
        var setter = layui.setter
            ,common = layui.common
            ,table = layui.table
            ,commons = layui.commons
            ,totalFilter=layui.totalFilter;
        //选中的组织
        var selectOrgId;
        //已选择的用户
        var roleList = [];
        var defaultUserList = [];
        if(roleList.length>0){
            defaultUserList = defaultUserList.concat(roleList);
        }
        //加载组织树
        loadOrgTree();
        function loadOrgTree() {
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "sysOrg/list",
                type: "get",
                contentType: 'application/json',
                dataType: 'json',
                success: function (data) {
                    if (commons._filterRecord(data)) {
                        var zNodes = data.data;
                        var setting = {
                            view: {
                                showLine: false,
                                nameIsHTML: true
                            },
                            edit: {
                                enable: false,
                                editNameSelectAll: false
                            },
                            data: {
                                simpleData: {
                                    enable: true,
                                    idKey:"orgId",
                                    pIdKey:"orgSupId"
                                }
                            },
                            callback: {
                                onClick: onClickOrgTree
                            }
                        };
                        $.fn.zTree.init($('#orgTree'), setting, zNodes);
                    } else {
                        window.parent.layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
                },
            });
        }

        //组织数点击事件
        function onClickOrgTree(event, treeId, treeNode, clickFlag) {
            selectOrgId = treeNode.orgId;
            common.tableConditionRender("roleListTable","queryForm",{where:{"orgId":treeNode.orgId}},true);
        }

        table.render({
            elem: '#roleListTable',
            method: 'get',
            url: serverPath.systemPath + setter.sysLinkPath + 'sysRole/list',
            page: true,
            height: 'full-10',
            where: {},
            defaultToolbar:[],
            toolbar:'#headerManagementRole',
            cols: [
                [
                    {type: 'checkbox'},
                    {field: 'roleId', title: 'ID', sort: true, hide: true},
                    {field: 'roleName', title: '角色', width: 160,sort: false},
                    {field: 'systemName', width: 230, title: '子系统',sort: false}
                ]
            ],
            parseData: function (res) {
                var _data = res.data;
                if (_data.length > 0 && roleList.length > 0) {
                    for (var i = 0; i < _data.length; i++) {
                        var _d = _data[i];
                        for (var j = 0; j < roleList.length; j++) {
                            var _td = roleList[j];
                            if (_d.roleId === _td.roleId) {
                                _d.LAY_CHECKED = true;
                            }
                        }
                    }
                }
                $(".contentDivs").html("");
                for (var i = 0; i < roleList.length; i++) {
                    $(".contentDivs").append("<tr><td>" +"<span>" +roleList[i].name +"</span>"+ " <i selectIds="+"'"+roleList[i].roleId+"'"+
                        "class='layui-icon' style='position:absolute;right:12px;cursor: pointer'>&#xe640;</i></td></tr>");
                }
            }
        });
        totalFilter.init('roleListTable');
        if(layui.roleList) {
            roleList.push.apply(roleList,layui.roleList)
            $("input[name=roleList]").val(JSON.stringify(roleList));
        };
        table.on('toolbar(roleListTable)', function (obj) {
            switch (obj.event) {
                case 'query':
                    common.tableConditionRender("roleListTable","queryForm",{where:{"orgId":selectOrgId}},false);
                    break;
            }
        });

        $(document).on("click",".center .layui-table-body table.layui-table tbody tr",function(){
            var obj = event ? event.target : event.srcElement;
            var tag = obj.tagName;
            var checkbox = $(this).find("td div.laytable-cell-checkbox div.layui-form-checkbox I");
            if(checkbox.length!=0){
                if(tag == 'DIV') {
                    checkbox.click();
                }
            }
        });
        $(document).on("click",".center td div.laytable-cell-checkbox div.layui-form-checkbox",function(e){
            e.stopPropagation();
        });

        table.on('checkbox(roleListTable)', function (obj) {
            var data = obj.data;
            var sel = obj.checked; //复选框状态
            var allS = obj.type; //是否全选
            var msg = {
                name: data.roleName,
                roleId: data.roleId
            };
            var arr_re = JSON.stringify(roleList).indexOf(JSON.stringify(msg)); //判断有没有
            var arrAll = table.cache.roleListTable;
            var index = 0;
            //全选
            if (allS == 'all') {
                if (sel) {
                    for (var i = 0; i < arrAll.length; i++) {
                        var msgs = {
                            name: arrAll[i].roleName,
                            roleId: arrAll[i].roleId
                        }
                        var arr_res = JSON.stringify(roleList).indexOf(JSON.stringify(msgs));
                        if (arr_res == -1) {
                            roleList.push(msgs)
                        }
                    }
                } else {
                    for (var i = 0; i < roleList.length; i++) {
                        for (var j = 0; j < arrAll.length; j++) {
                            if (roleList[i].roleId == arrAll[j].roleId) {
                                roleList.splice(i, 1);
                                i--;
                                break;
                            }
                        }
                    }
                }
            } else {
                if (sel) {
                    if (arr_re == -1) {
                        roleList.push(msg);
                    }
                } else {
                    if (arr_re != -1) {
                        for (var i = 0; i < roleList.length; i++) {
                            if (roleList[i].roleId == msg.roleId) {
                                index = i;
                                break;
                            }
                        }
                        roleList.splice(index, 1);
                    }
                }
            }
            $(".contentDivs").html("");
            for (var i = 0; i < roleList.length; i++) {
                $(".contentDivs").append("<tr><td>" +"<span>" +roleList[i].name +"</span>"+ " <i selectIds="+"'"+roleList[i].roleId+"'"
                    + "class='layui-icon' style='position:absolute;right:12px;cursor: pointer'>&#xe640;</i></td></tr>");
            }
            $("input[name=roleList]").val(JSON.stringify(roleList));
        });

        $(document).on("click",".contentDivs .layui-icon",function() {
            $(this).parent().parent().remove();
            var delData = $(this).attr('selectIds');
            for (var i = 0; i < roleList.length; i++) {
                if (roleList[i].roleId == delData) {
                    roleList.splice(i, 1);
                }
            }

            table.reload("roleListTable", {
                parseData: function (res) {
                    var _data = res.data;
                    if (_data.length > 0 && roleList.length > 0) {
                        for (var i = 0; i < _data.length; i++) {
                            var _d = _data[i];
                            for (var j = 0; j < roleList.length; j++) {
                                var _td = roleList[j];
                                if (_d.roleId === _td.roleId) {
                                    _d.LAY_CHECKED = true;
                                }
                            }
                        }
                    }
                },
                page: {curr: 1}
            });
        });
        $(document).on("click",".contentDivs .layui-icon",function(e){
            e.stopPropagation();
            $("input[name=roleList]").val(JSON.stringify(roleList));
        });

        //全部取消
        window.cancelData = function(){
            $(".contentDivs").html("");
            roleList = [];
            table.reload('roleListTable');
        };
    });
</script>
<style>
    .left {
        width: 26%;
        display: inline-block;
        float: left;
    }

    .center {
        width: 69%;
        display: inline-block;
        margin-left: 15px;
        float: left;
    }

    ul.ztree {
        background: #fff;
        width: 100%;
        height: 473px;
        overflow: auto;
    }

    .filter-table .layui-form.layui-border-box.layui-table-view {
        position: absolute;
        margin: 0
    }
    .center .layui-table-page{
        padding-left: 2px;
        padding-right: 2px;
        width: 495px;  /*表格分页底部  */
    }
    .contentDivs td{
        height: auto;
        padding-right: 30px;
    }
    .contentDivs span{
        word-wrap: break-word;
        word-break: break-all;
        overflow: hidden;
    }
    .center .layui-table-view .layui-table{
        width: 100%; /*表格主体宽度*/
    }
</style>
</body>
</html>